<template>
	<view class="box">
		<!-- 以下地址为当前订单地址 -->
		<view class="now" v-if="orgin==2">
			<view class="title f28 bold">以下地址为当前订单地址</view>
			<!-- 地址 -->
			<view class="list">
				<view @click="sureAddress(now_order)">
					<view class="one f-w">
						<view class="left hide-line">
							<view class="tag" v-if="now_order.isDefult">默认</view>
							{{now_order.address}}
						</view>
						<view class="icon" @click.stop="updateAddress(now_order.id)">
							<image src="https://jw.xzsw2021.com/static/client/icon_59@2x.png" mode=""></image>
						</view>
					</view>
					<!-- 姓名-电话 -->
					<view class="two f-w">
						<view class="name">
							{{now_order.username}}
						</view>
						<view class="mobile">
							{{now_order.mobile}}
						</view>
					</view>
				</view>
			</view>
			<view class="bg"></view>
		</view>
		<!-- 正常收货地址列表 -->
		<view class="list" v-if="list.length>0">
			<view v-for="(item,index) in list" :key="index" @click="marketList(item)">
				<view class="one f-w">
					<view class="left hide-line">
						<view class="tag" v-if="item.is_default">默认</view>
						{{(item.detail) || '暂无'}}
					</view>
					<view class="icon" @click.stop="edit(item)">
						<image src="https://jw.xzsw2021.com/static/client/icon_59@2x.png" mode=""></image>
					</view>
				</view>
				<!-- 姓名-电话 -->
				<view class="two f-w">
					<view class="name">
						{{item.real_name}}
					</view>
					<view class="mobile">
						{{item.phone}}
					</view>
				</view>
			</view>
		</view>
		<view class="none" v-else>
			<view class="nimg">
				<image src="https://jw.xzsw2021.com/static/client/icon_55@2x.png" mode=""></image>
			</view>
			<view class="desc">您还没有收货地址</view>
			<view class="btn2" @click="goAddDress">新建地址</view>
		</view>
		<!-- 当传经纬度超出配送范围地址列表 -->
		<view class="bg" v-if="false"></view>
		<view class="list list2" v-if="false">
			<view class="range">
				<view class="f28 bold">以下地址超出配送区域</view>
				<view class="f22 orange">可配送区域为重庆市，请修改或新增您的地址</view>
			</view>
			<view v-for="(item,index) in list" :key="index" @click.stop="updateAddress(item.id)">
				<view class="one f-w">
					<view class="left hide-line">
						<view class="tag" v-if="item.isDefult">默认</view>
						{{item.address}}
					</view>
					<view class="icon">
						<image src="https://jw.xzsw2021.com/static/client/icon_59@2x.png" mode=""></image>
					</view>
				</view>
				<!-- 姓名-电话 -->
				<view class="two f-w">
					<view class="name">
						{{item.username}}
					</view>
					<view class="mobile">
						{{item.mobile}}
					</view>
				</view>
			</view>
		</view>
		<u-picker @cancel="show = false" @confirm="sureShop" :show="show" :columns="columns" confirmColor="#22B591"
			keyName="mar_name" immediateChange></u-picker>
		<!-- <u-picker @cancel="show = false" @confirm="sureShop" :show="show" :columns="columns" confirmColor="#22B591"
			keyName="mar_name"></u-picker> -->
		<!-- 添加收货地址 -->
		<view class="btn" @click="goAddDress" v-if="list.length>0">
			添加收货地址
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getUserAddressApi
	} from '@/api/user.js'
	import {
		getMarketListApi
	} from '@/api/market.js'
	import Cache from '@/util/cache.js'
	export default {
		data() {
			return {
				columns: [], //菜市场列表
				show: false,
				list: [], // 收货地址列表
				orgin: '', //1.选择地址 2.修改地址
				kind: '', //1 首页进来
				type: '', //满汉全席活动兑换
				page: 1,
				isMore: true,
				now_order: {
					id: 2,
					username: '柠檬李',
					mobile: '18325074236',
					address: '北京市朝阳区三环到四环之间 东三环南里 华腾',
					isDefult: false
				},
				addressSearch: {
					page: 1,
					limit: 10,
				},
				marketSearch: { //搜索菜市场
					user_coordinate: 1,
					lng: '',
					lat: ''
				},
				m_product_id: '',
				m_unique: '',
				m_id: '',
				todaystar: '',
				todayend: ''
			}
		},
		onReachBottom() {

		},
		onLoad(options) {
			if (options.orgin) {
				this.orgin = options.orgin
			}
			if (options.kind) {
				this.kind = options.kind
			}

			this.type = options.type ? options.type : ''
			if (this.type == 'manhan') {
				this.m_product_id = options.product_id
				this.m_unique = options.unique
				this.m_id = options.activeid
				this.gettoday()
			}

		},
		onShow() {
			this.addressList()
		},
		methods: {
			addressList(itme) {
				getUserAddressApi(this.addressSearch).then(res => {
					this.list = res.data.list
				}).catch(err => {

				})
			},
			// 确认切换店铺
			sureShop(e) {
				let pages = getCurrentPages();
				pages.forEach(page => {
					page.$vm.myshop = e.value[0]
				})
				this.$forceUpdate()
				uni.navigateBack({

				})
			},
			marketList(item) {
				Cache.set('user_address', item)
				if (this.kind == 1) {
					this.marketSearch.lat = item.latitude
					this.marketSearch.lng = item.longitude
					if (item.latitude <= 0 || item.longitude <= 0) {
						this.$refs.uToast.show({
							type: 'default',
							title: '默认主题',
							message: '请先完善收货信息',
						})
						return;
					}
					uni.showLoading({
						mask: true
					})
					getMarketListApi(this.marketSearch).then(res => {
						this.columns = [res.data]
						uni.hideLoading()
						this.show = true
					}).catch(err => {
						uni.hideLoading()
					})
				}
				if (this.orgin == 1) {
					let pages = getCurrentPages();
					pages.forEach(page => {
						page.$vm.address = item;
					})
					// let page = pages[pages.length - 2];

					uni.navigateBack();
				}

				if (this.type) {
					this.manhanorder(item.address_id)

				}
			},
			// 满汉全席下单
			manhanorder(address_id) {
				let data = {
					id: this.m_id,
					product_id: this.m_product_id,
					product_attr_unique: this.m_unique,
					address_id: address_id
				}

				this.$request('user/banquet/order/check', data).then(res => {
					if (res.status == 200) {
						this.payorder(res.data.cart_id, address_id)
					}
				})
				// uni.redirectTo({
				// 	url: '/pages/state/success/success?type=12'
				// })
			},
			payorder(cart_id, address_id) {
				uni.showModal({
					title: '提示',
					content: '确认兑换商品?',
					success: res => {
						if (res.confirm) {
							let data = {
								cart_id: cart_id,
								id: this.m_id,
								buyer_type: 0,
								address_id: address_id,
								pay_type: 'routine',
								delivery_start_date: this.todaystar,
								delivery_end_date: this.todayend,
								delivery_time_scope: '00:00-23:59'
							}

							this.$request('user/banquet/order/create', data).then(res => {
								if (res.status == 200) {
									uni.redirectTo({
										url: '/pages/state/success/success?type=12'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});




				// uni.redirectTo({
				// 	url: '/pages/state/success/success?type=12'
				// })
			},
			gettoday() {
				let today = new Date();
				let year = today.getFullYear();
				let month = today.getMonth() + 1;
				let day = today.getDate();
				let hours = today.getHours();
				this.todaystar = `${year}-${month}-${day} 00:00:00`
				this.todayend = `${year}-${month}-${day} 23:59:59`



			},
			// 确认选择收货地址
			sureAddress(item) {
				console.log('sdddddddddddddddddddddddd')

				if (this.kind == 1) {
					let data = {
						lat: item.latitude,
						lng: item.longitude
					}
					uni.showLoading({
						mask: true
					})
					this.$request('market/lst', data, 'GET').then(res => {
						if (res.status == 200) {
							uni.hideLoading()
							this.show = true
							this.columns = [res.data]
						}


					})
				}


				if (this.orgin) {
					let pages = getCurrentPages();
					let page = pages[pages.length - 2];
					page.$vm.address = item;

					uni.navigateBack();
				} else {
					// uni.navigateTo({
					// 	url:'/pages/recovery/address-edit/address-edit?type=1&address_id='+address_id
					// })
				}
				// 满汉活动
				if (this.type) {
					uni.redirectTo({
						url: '/pages/state/success/success?type=12'
					})
				}
			},
			// 编辑
			edit(data) {
				let a = JSON.stringify(data);
				console.log(a, 'data');
				uni.navigateTo({
					url: `/pages/client/address-add/address-add?data=${a}`
				})
			},

			// 新建地址
			goAddDress() {
				uni.navigateTo({
					url: '/pages/client/address-add/address-add'
				})
			},
		}
	}
</script>
<style>

</style>

<style lang="scss" scoped>
	.none {
		margin-top: 188rpx;
		overflow: hidden;
		text-align: center;

		.nimg {
			margin: 0 auto;
			width: 352rpx;
			height: 352rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.desc {
			margin: 45rpx auto 68rpx;
			font-size: 30rpx;
			color: #666666;
		}

		.btn2 {
			margin: 0 auto;
			width: 300rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 32rpx;
			color: #fff;
			background: linear-gradient(64deg, #2BCCA5, #22B591);
			box-shadow: 0rpx 4rpx 6rpx 0rpx rgba(20, 187, 146, 0.4);
			border-radius: 10rpx;
		}
	}

	.box {
		padding-bottom: 120rpx;

		.orange {
			color: #F56624;
		}

		.now {
			.title {
				padding: 28rpx 30rpx;
			}

			.list {
				&>view {
					padding: 0 0 30rpx;
				}
			}
		}

		.list {
			padding: 0 30rpx;
			background-color: #fff;

			&>view:not(:last-child) {
				border-bottom: 1rpx solid #eee;
			}

			&>view {
				padding: 30rpx 0;
				overflow: hidden;

				.one {
					// margin-top: 30rpx;
				}

				.left {
					flex: 1;
					font-size: 28rpx;
				}

				.tag {
					display: inline-block;
					margin-right: 10rpx;
					width: 44rpx;
					padding: 2rpx;
					font-size: 22rpx;
					background: #DEFFF7;
					border-radius: 4px;
					font-weight: 400;
					color: #109C7A;
				}
			}

			.icon {
				margin-left: 30rpx;
				flex-shrink: 0;
				width: 30rpx;
				height: 30rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.two {
				margin-top: 20rpx;
				color: #666;
				font-size: 26rpx;
			}

			.mobile {
				margin-left: 15rpx;
			}
		}

		.bg {
			width: 750rpx;
			height: 20rpx;
			background: #F5F5F5;
			box-sizing: border-box;
		}

		.list2 {
			&>view {
				border-bottom: 1rpx solid #eee;
			}

			.range {
				border: none !important;
				padding-bottom: 0;

				&>view:nth-child(1) {
					margin-bottom: 10rpx;
				}
			}

			.one,
			.two {
				color: #999;
			}
		}

		.btn {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 750rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #fff;
			font-size: 32rpx;
			background: linear-gradient(64deg, #2BCCA5, #22B591);
		}
	}
</style>
